<template>
  <view class="search-box">
    <uni-search-bar @input="input" radius="100"  cancel-button="none" :focus="true"></uni-search-bar>
  </view>
  <view class="sugg-list">
    <view class="sugg-item" v-for="(item,index) in searchResult" :key="item.goods_id" @click="gotoItemDetail(item)">
      <view class="good-name">{{item.goods_name}}</view>
      <uni-icons type="arrowright" size="16"></uni-icons>
    </view>
  </view>
  <view class="history-box" v-if="searchResult.length===0&&historyList.length!==0">
    <view class="history-title">
      <text>搜索历史</text>
      <uni-icons type="trash" size="17" @click="clean"></uni-icons>
    </view>
    <view class="histor-list">
      <uni-tag class="tag" v-for="(item,index) in history" :key="index" :text="item" @click="gotoGoodsList(item)"></uni-tag>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchResult:[],
        historyList: []
      };
    },
    computed:{
      history(){
        return [...this.historyList].reverse()
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('uniShopHistoryList')||'[]')
    },
    methods:{
      input(e){
        this.timer&&clearTimeout(this.timer)
        this.timer=setTimeout(()=>{
          this.kw = e
          if(!this.kw){
            this.searchResult=[]
            return
          }
          this.getSearchList(this.kw)
          this.saveHistoryList(this.kw)
        },500)
      },
      async getSearchList(word){
        const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:word})

        if(res.meta.status!==200){
          return uni.$showMsg()
        }
        this.searchResult = res.message
      },
      gotoItemDetail(item){
        uni.navigateTo({
          url:`/subpackage/goods_detail/goods_detail?goods_id=${item.goods_id}`
        })
      },
      gotoGoodsList(item){
        uni.navigateTo({
          url:`/subpackage/goods_list/goods_list?query=${item}`
        })
      },
      saveHistoryList(word){
        const set = new Set(this.historyList)
        set.delete(word)
        set.add(word)
        this.historyList = Array.from(set)
        uni.setStorageSync('uniShopHistoryList',JSON.stringify(this.historyList))
      },
      clean(){
        this.historyList = []
        uni.setStorageSync('uniShopHistoryList','[]')
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0px;
  z-index: 999;
  background-color: #C00000
}
.sugg-item{
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 13px 0;
  border-bottom: 1px solid #efefef;
  .good-name{
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.history-box{
  padding: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .histor-list{
    display: flex;
    flex-wrap: wrap;
    .tag{
      margin: 5px 5px 0 0;
    }
  }
}
</style>
